$(function () {
    "use strict";
    $.ajax({
        url: 'Repeat/getnotifications',
        type: 'post',
        success: function (result) {
            var html = '';
            $.each(result.data, function (key, value) {
                html += '<li class="stream-item" id="' + value.id + '"><div class="stream-item-detail"><div class="stream-item-content"><div class="stream-item-header"><a class="account-group" href="' + value.username + '"><img class="stream-item-avater size48" src="' + value.avatar + '"><strong class="fullname">' + value.nickname + '</strong><span>&rlm;</span><span class="username">@<b>' + value.username + '</b></span></a><small class="time"><span class="timestamp"><span class="_timestamp">' + value.datetime + '</span></span></span></small></div><div class="stream-item-text-container"><p class="stream-item-text">' + value.content + '</p><div class="p-send"><div class="send-box-bar"><button id="repeat-btn2" class="send-box-btn repeat-btn2" type="button">回复</button></div></div></div></div></div></li>';
            });
            $('#therepeat').html(html);
            $(".stream-item .send-box-btn").on('click', function (event) {
                event.stopPropagation();
                event.preventDefault();
                var t = $(this).parent().parent().parent().parent().parent().parent();
                var reid = t.attr('id');
                $.ajax({
                    url: 'Repeat/getwhorepeat',
                    type: 'post',
                    data: {
                        towho: reid
                    }, success: function (result) {
                        var html = '';
                        var h3 = '';
                        $.each(result.data, function (key, value) {
                            html = '<div class="list-li" id="' + value.who + '"><a class="account-group" href="' + value.username + '"><img class="stream-item-avater size48" src="' + value.avatar + '"><strong class="fullname">' + value.nickname + '</strong><span>&rlm;</span><span class="username">@<b>' + value.username + '</b></span></a><small class="time"><span class="timestamp"><span class="_timestamp">' + value.datetime + '</span></span></span></small><div class="stream-item-text-container"><p class="stream-item-text">' + value.content + '</p></div>';
                            h3 = '回复@' + value.username + '';
                        });
                        $('#global-repeat-box').find('.modal-title').html(h3);
                        h3 += '：';
                        $('#global-repeat-box').find('.send-box-textarea').html(h3);
                        $('.modal-repeat').html(html);
                    }
                });
                $('#global-repeat-box').css("display", "block");
                $('.global-dialog').show();

                $("#global-repeat-box .send-box-textarea").on("input", function () {
                    var towho = $('#global-repeat-box').find('.modal-title').text();
                    towho += '：';
                    var hftext = $(this).val();
                    var s = new RegExp(/^\s+$/);
                    var num = $(this).val().length;
                    $('#global-repeat-box .counter').html(140 - num);

                    if (num === 0 || num > 140) {
                        $("#global-repeat-box .send-box-btn").attr("disabled", "disabled");
                    } else {
                        if (s.test(hftext)) {
                            $("#global-repeat-box .send-box-btn").attr("disabled", "disabled");

                        } else if (towho == hftext) {
                            $("#global-repeat-box .send-box-btn").attr("disabled", "disabled");
                        } else {
                            $("#global-repeat-box .send-box-btn").removeAttr("disabled");
                        }
                    }
                });
                $("#global-repeat-box .send-box-btn").on('click', function () {
                    $("#global-repeat-box .send-box-btn").attr("disabled", "disabled");
                    var content = $('#global-repeat-box .send-box-textarea').val();
                    var tocomment = t.attr('id');
                    var towho = $('#global-repeat-box').find('.username').find('b').text();
                    $.ajax({
                        url: 'Repeat/sendrepeat',
                        type: 'post',
                        data: {
                            content: content,
                            tocomment: tocomment,
                            towho: towho
                        },
                        success: function (result) {
                            t.find(".repeat-textarea").val("");
                            $('#global-repeat-box .send-box-textarea').val("");
                        }
                    });
                    $('#global-repeat-box').css("display", "none");
                    $(".global-dialog").hide();
                });
            });

        }
    });
});